from django.contrib.auth.models import User
from django.db import models

from utils.aescharfield import AesCharField
from lxml.html._diffcommand import description
from utils.storage import Storage


# Create your models here.
class Fileupload(models.Model):
    EXEC_TYPE = (
        (0,'上传'),
        (1,'下载'),
    )
    RESULT_STAUTS = (
        (0,'成功'),
        (1,'失败'),
        (2,'未执行'),
    )
    srcfile = models.FileField(upload_to='%Y%m%d',verbose_name="本地目录",storage=Storage())
    dstfile = models.CharField(max_length=200,verbose_name="远程目录")
    uploadtime = models.DateTimeField(auto_now=True,verbose_name="执行时间")
    exec_dst_host = models.ManyToManyField("VodHost",related_name="fileupload_host",verbose_name="目标主机")
    uploaduser = models.ForeignKey(User,on_delete=models.CASCADE,related_name="fileupload_user",verbose_name="执行人员")
    exec_type = models.PositiveIntegerField(choices=EXEC_TYPE,default=0,verbose_name="类型")
    exec_result = models.PositiveIntegerField(choices=RESULT_STAUTS,default=2,verbose_name="结果")
    exec_result_detail = models.CharField(max_length=2000,verbose_name="具体信息")
    
    class Meta:
        verbose_name = u'文件上传/下载'
        verbose_name_plural = u'文件上传/下载'
        
class VodHost(models.Model):
    ONLINE_STATUS = (
        (0,'在线'),
        (1,'离线'),
    )
    vod_ip = models.CharField(max_length=15,verbose_name="ip地址")
    vod_hostname = models.CharField(max_length=50,verbose_name="主机名")
    vod_online = models.PositiveIntegerField(choices=ONLINE_STATUS,default=0,verbose_name="主机状态")
    vod_description = models.CharField(max_length=200,blank=True,null=True,verbose_name="描述")
    vod_account = models.ForeignKey("HostSystemAccount",on_delete=models.CASCADE,related_name="vodhost_account",verbose_name="系统账号")
    vod_ssh_port = models.IntegerField(default=22,verbose_name="SSH端口")
    class Meta:
        verbose_name = u'集群主机管理'
        verbose_name_plural = u'集群主机管理'
    def __str__(self):
        return self.vod_hostname+'-'+self.vod_ip
    
class Command(models.Model):
    RESULT_STAUTS = (
        (0,'成功'),
        (1,'失败'),
        (2,'未执行'),
    )
    exec_command = models.CharField(max_length=200,verbose_name="命令")
    exec_dst_host = models.ManyToManyField(VodHost,related_name="command_host",verbose_name="目标主机")
    exec_user = models.ForeignKey(User,on_delete=models.CASCADE,related_name="command_user",verbose_name="执行人员")
    exec_result = models.PositiveIntegerField(choices=RESULT_STAUTS,default=2,verbose_name="执行结果")
    exec_result_detail = models.CharField(max_length=2000,verbose_name="结果明细")
    exec_time = models.DateTimeField(auto_now=True,verbose_name="执行时间")
    
    class Meta:
        verbose_name = u'命令执行'
        verbose_name_plural = u'命令执行'
        
        
class Wiki(models.Model):
    title = models.CharField(max_length=50,verbose_name="标题")
    contentarea = models.TextField(verbose_name="内容")
    
    def __str__(self):
        return self.title
    
    class Meta:
        verbose_name = u'知识库管理'
        verbose_name_plural = u'知识库管理'   

class HostSystemAccount(models.Model):
    name = models.CharField(max_length=20,verbose_name="名称")
    username = models.CharField(max_length=20,verbose_name="账号")
    pwd = AesCharField(max_length=50,verbose_name="密码")
    description = models.CharField(max_length=50,blank=True,verbose_name="备注")
    
    def __str__(self):
        return self.name+'|'+self.username
    
    
    class Meta:
        verbose_name = u'系统账号管理'
        verbose_name_plural = u'系统账号管理'

class DeployClusterClient(models.Model):
    pass



        